package work20241126_27;

import java.util.Arrays;
import java.util.Scanner;

/**
 * @author zdl
 * @date 2024/11/26
 */
public class Demo24 {
    public static void main(String[] args) {
        //实现一个方法，删除数组中的指定元素。
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个整数：");
        int num = sc.nextInt();
        delArr(num);
    }
    public static void delArr(int num){
        int[] arr = {1,2,3,2,3,1,4,5,6};
        int count = 0;//记录删除值的次数
        for (int i = 0; i < arr.length; i++) {
            if (num == arr[i]){
                count++;
            }
        }
        if (count == 0){
            System.out.println("数组中没有找到要删除的元素:"+num);
            System.out.println("原数组:"+Arrays.toString(arr));
            return;
        }
        int index = 0;
        int[] new_arr = new int[arr.length - count];
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] != num){
                new_arr[index++] = arr[i];
            }
        }
        System.out.println("删除"+num+"前的数组为:"+Arrays.toString(arr));
        System.out.println("删除"+num+"后的数组为:"+Arrays.toString(new_arr));
    }
}
